import { GCellRenderer } from "../../../../com/mui/controls/GCellRenderer";
import { GTextFormat } from "../../../common/GTextFormat";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { BaseItem } from "../common/BaseItem";
import { ObjCreate } from "../common/ObjCreate";
import { Language } from "../../../../extend/language/Language";
import { SMarketItem } from "../../../../Message/Game/SMarketItem";
import { EMarketRecordType } from "../../../../Message/Game/EMarketRecordType";
import { EPrictUnit } from "../../../../Message/Public/EPrictUnit";
import { ItemData } from "../../resource/info/ItemData";
import { MagicWeaponUtil } from "../magicWeapon/MagicWeaponUtil";
import { StringHelper } from "../../../../com/gengine/utils/StringHelper";
import { JSON } from "../../../../com/mui/serialization/json/JSON";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { ECategory } from "../../../../Message/Public/ECategory";
import { MoneyUtil } from "../common/util/MoneyUtil";
import { ColorConfig } from "../../resource/ColorConfig";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { Cache } from "../../cache/Cache";
import { PulseSharedObject } from "../player/PulseSharedObject";
import { MsgManager } from "../../manager/MsgManager";
import { SellGoodsTips } from "./SellGoodsTips";
import { TipsWindow } from "../player/TipsWindow";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { GButton } from "../../../../com/mui/controls/GButton";
type int = number;
//class MarketSearchRenderer
    
    export  class MarketSearchRenderer extends GCellRenderer
    {
        constructor()
        {
            super();
            return;
        }

        protected /* override */ configUI(): void
        {
            this._blueTf = new GTextFormat("", 12, 11661311);
            this._coinImg = GlobalClass.getBitmap(ImagesConst.Market_Coin);
            this._coinImg.x = 10;
            this._coinImg.y = 10;
            this._coinImg.visible = false;
            addChild(this._coinImg);
            this._goldImg = GlobalClass.getBitmap(ImagesConst.Market_Gold);
            this._goldImg.x = 10;
            this._goldImg.y = 10;
            this._goldImg.visible = false;
            addChild(this._goldImg);
            this._item = new BaseItem();
            this._item.setSize(32, 32);
            this._item.move(10, 10);
            addChild(this._item);
            this._nameTxt = this.createTextfield(45, 20, 100, 20, "", this._blueTf);
            this._levelTxt = this.createTextfield(145, 20, 40, 20, "", this._blueTf);
            this._priceTxt = this.createTextfield(235, 20, 60, 20, "", this._blueTf);
            this._sellerTxt = this.createTextfield(270, 20, 90, 20, "", new GTextFormat("", 12, 11661311, null, null, null, null, null, flash.text.TextFormatAlign.CENTER));
            this._buyBtn = ObjCreate.createGButton(Language.getString(31039), 410, 15, 50, 22, this);
            this._buyBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBuyBtnClickHandler);
            this._buyBtn.drawNow();
            this._coinIcon = GlobalClass.getBitmap(ImagesConst.Tongqian);
            this._coinIcon.x = 210;
            this._coinIcon.y = 20;
            addChild(this._coinIcon);
            this._goldIcon = GlobalClass.getBitmap(ImagesConst.Yuanbao);
            this._goldIcon.x = 210;
            this._goldIcon.y = 20;
            addChild(this._goldIcon);
            var loc1=ObjCreate.createBg(10, 50, 502, 2, this, ImagesConst.SplitLine);
            return;
        }

        /* internal  */createTextfield(arg1: int, arg2: int, arg3: int, arg4: int, arg5: string, arg6: Object /* flash.text.TextFormat */): Object /* flash.text.TextField */
        {
            var loc1;
            (loc1 = new Object /* flash.text.TextField */()).x = arg1;
            loc1.y = arg2;
            loc1.width = arg3;
            loc1.height = arg4;
            loc1.selectable = false;
            loc1.mouseEnabled = false;
            loc1.defaultTextFormat = arg6;
            loc1.text = arg5;
            addChild(loc1);
            return loc1;
        }

        public /* override */ set data(arg1: Object)
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=0;
            var loc8=null;
            var loc9=null;
            super.data = arg1;
            this._item.itemData = null;
            this._coinImg.visible = false;
            this._goldImg.visible = false;
            this._sMarketItem = arg1 as SMarketItem;
            this._marketId = this._sMarketItem.marketId;
            this._ownerName = this._sMarketItem.ownerName;
            if (this._sMarketItem.recordType != EMarketRecordType._EMarketRecordTypeSell) 
            {
                if (this._sMarketItem.recordType == EMarketRecordType._EMarketRecordTypeSeekBuy) 
                {
                    this._buyBtn.label = Language.getString(31133);
                }
            }
            else 
            {
                this._buyBtn.label = Language.getString(31039);
            }
            var loc1=this._sMarketItem.code;
            if (loc1 != EPrictUnit._EPriceUnitCoin) 
            {
                if (loc1 != EPrictUnit._EPriceUnitGold) 
                {
                    loc3 = new ItemData(loc1);
                    if (MagicWeaponUtil.isFaultItem(loc3)) 
                    {
                        loc3.isFaultMagicWeapon = true;
                    }
                    this._item.itemData = loc3;
                    this._sMarketItem.itemExtend = StringHelper.trim(this._sMarketItem.itemExtend);
                    loc4 = "";
                    loc5 = "";
                    loc6 = "";
                    loc7 = 0;
                    if (!(this._sMarketItem.itemExtend == "") && !(this._sMarketItem.itemExtend == null) && !(this._sMarketItem.itemExtend == "null")) 
                    {
                        loc8 = JSON.deserialize(this._sMarketItem.itemExtend);
                        this._item.itemData.setItemExInfo(loc8);
                        this._item.itemData = this._item.itemData;
                        if (loc8.prefix) 
                        {
                            loc7 = loc8.prefix;
                        }
                        if ((loc9 = GameDefConfig.instance.getEPrefixx(loc7, loc3.type)) && loc3.itemInfo.item.category == ECategory._ECategoryEquip) 
                        {
                            loc5 = loc9.name + Language.getString(31038);
                        }
                        if (!(loc8.strengthen == undefined) && !(loc8.strengthen == 0)) 
                        {
                            loc6 = "+" + loc8.strengthen;
                        }
                    }
                    loc4 = loc5 + loc3.itemInfo.item.name + loc6;
                    this._item.amount = this._sMarketItem.amount;
                    this._nameTxt.text = loc4;
                }
                else 
                {
                    this._goldImg.visible = true;
                    this._nameTxt.text = MoneyUtil.getFormatInt(this._sMarketItem.amount) + Language.getString(31037);
                }
            }
            else 
            {
                this._coinImg.visible = true;
                this._nameTxt.text = MoneyUtil.getFormatInt(this._sMarketItem.amount) + Language.getString(31036);
            }
            this._levelTxt.text = this._sMarketItem.level + "";
            this._priceTxt.text = MoneyUtil.getFormatInt(this._sMarketItem.sellPrice) + "";
            var loc2=ColorConfig.instance.getItemColor(this._sMarketItem.color).intColor;
            this._nameTxt.setTextFormat(new GTextFormat("", 12, loc2));
            this._sellerTxt.text = this._ownerName;
            if (this._sMarketItem.ownerPlayerId == 0) 
            {
                this._sellerTxt.htmlText = "<font color=\'#00ff00\'>" + this._ownerName + "</font>";
            }
            if (this._sMarketItem.sellUnit != EPrictUnit._EPriceUnitCoin) 
            {
                if (this._sMarketItem.sellUnit == EPrictUnit._EPriceUnitGold) 
                {
                    this._coinIcon.visible = false;
                    this._goldIcon.visible = true;
                }
            }
            else 
            {
                this._coinIcon.visible = true;
                this._goldIcon.visible = false;
            }
            return;
        }

        /* internal  */onChatBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MarketPrivateChat, this._ownerName));
            return;
        }

        /* internal  */onBuyBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc4=0;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            var loc1=this._priceTxt.text;
            var loc2="";
            var loc3="";
            if (this._sMarketItem.sellUnit != EPrictUnit._EPriceUnitCoin) 
            {
                if (this._sMarketItem.sellUnit == EPrictUnit._EPriceUnitGold) 
                {
                    loc1 = loc1 + " " + Language.getString(31037);
                    loc2 = ImagesConst.Yuanbao_small;
                    if (this._sMarketItem.recordType != EMarketRecordType._EMarketRecordTypeSell) 
                    {
                        loc3 = "MarketGoldSellGoods";
                    }
                    else 
                    {
                        loc3 = "MarketGoldBuyGoods";
                    }
                }
            }
            else 
            {
                loc1 = loc1 + " " + Language.getString(31036);
                loc2 = ImagesConst.Tongqian_small;
                if (this._sMarketItem.recordType != EMarketRecordType._EMarketRecordTypeSell) 
                {
                    loc3 = "MarketCoinSellGoods";
                }
                else 
                {
                    loc3 = "MarketCoinBuyGoods";
                }
            }
            if (this._sMarketItem.sellUnit == EPrictUnit._EPriceUnitCoin && this._sMarketItem.recordType == EMarketRecordType._EMarketRecordTypeSell && this._sMarketItem.sellPrice > Cache.instance.role.money.coin) 
            {
                loc4 = this._sMarketItem.sellPrice - Cache.instance.role.money.coin;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, loc4));
            }
            else if (PulseSharedObject.isTodayNotTips(loc3)) 
            {
                if (this._sMarketItem.recordType == EMarketRecordType._EMarketRecordTypeSell) 
                {
                    this.marketSureToBuy();
                }
            }
            else 
            {
                loc5 = ColorConfig.instance.getItemColor(this._sMarketItem.color).color;
                loc6 = "";
                if (this._sMarketItem.code == EPrictUnit._EPriceUnitCoin || this._sMarketItem.code == EPrictUnit._EPriceUnitGold) 
                {
                    if (this._sMarketItem.recordType == EMarketRecordType._EMarketRecordTypeSell) 
                    {
                        loc6 = Language.getStringByParam(31040, loc5, this._nameTxt.text, loc1);
                    }
                }
                else if (this._sMarketItem.recordType == EMarketRecordType._EMarketRecordTypeSell) 
                {
                    loc6 = Language.getStringByParam(31041, this._sMarketItem.amount, loc5, this._nameTxt.text, loc1);
                }
                if (this._sMarketItem.recordType != EMarketRecordType._EMarketRecordTypeSell) 
                {
                    if (this._sMarketItem.recordType == EMarketRecordType._EMarketRecordTypeSeekBuy) 
                    {
                        loc7 = (loc7 = (loc7 = Cache.instance.pack.backPackCache.getNotBindItemByCode(this._sMarketItem.code)).concat(Cache.instance.pack.stonePackCache.getNotBindItemByCode(this._sMarketItem.code))).concat(Cache.instance.pack.magicWeaponPackCache.getNotBindItemByCode(this._sMarketItem.code));
                        if ((loc7 = this.filterMarketesAmulet(loc7)) && loc7.length <= 0) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(31127));
                            if (!SellGoodsTips.instance.isHide) 
                            {
                                SellGoodsTips.instance.hide();
                            }
                            return;
                        }
                        SellGoodsTips.instance.show();
                        SellGoodsTips.instance.updateInfo(this._sMarketItem.color, this._sMarketItem.sellPrice, this._sMarketItem.sellUnit, loc7, this._sMarketItem.amount, this.marketSureToSell);
                    }
                }
                else 
                {
                    TipsWindow.instance.show();
                    TipsWindow.instance.updateInfo(loc6, loc3, "", this.marketSureToBuy);
                    TipsWindow.instance.createImgAndName(loc2, Language.getString(31039));
                    TipsWindow.instance.setNotTipsVisible(true);
                }
            }
            return;
        }

        /* internal  */filterMarketesAmulet(arg1: Array<any>): Array<any>
        {
            var loc2=null;
            var loc3=0;
            var loc1=[];
            if (arg1.length > 0) 
            {
                loc3 = 0;
                while (loc3 < arg1.length) 
                {
                    loc2 = arg1[loc3] as ItemData;
                    if (loc2.category != ECategory._ECategoryAmulet) 
                    {
                        loc1.push(loc2);
                    }
                    else if (!loc2.isHasIdentify()) 
                    {
                        loc1.push(loc2);
                    }
                    ++loc3;
                }
            }
            return loc1;
        }

        /* internal  */marketSureToBuy(): void
        {
            if (this._sMarketItem.recordType == EMarketRecordType._EMarketRecordTypeSell) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MarketBuyGoods, this._marketId));
            }
            return;
        }

        /* internal  */marketSureToSell(): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=null;
            if (this._sMarketItem.recordType == EMarketRecordType._EMarketRecordTypeSeekBuy) 
            {
                loc1 = Cache.instance.pack;
                loc2 = SellGoodsTips.instance.itemUid;
                loc3 = SellGoodsTips.instance.amount;
                if (!(loc4 = SellGoodsTips.instance.itemCategory != ECategory._ECategoryAmulet ? loc1.backPackCache.getItemDataByUid(loc2) : loc1.magicWeaponPackCache.getItemDataByUid(loc2))) 
                {
                    loc4 = Cache.instance.pack.stonePackCache.getItemDataByUid(loc2);
                }
                if (loc4 && !ItemsUtil.isBind(loc4)) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MarketSellGoods, {"marketId": this._sMarketItem.marketId, "uid": loc2, "amount": loc3}));
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(31127));
                }
            }
            return;
        }

        /* internal  */onMailBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MailSendToPlayer, this._ownerName));
            return;
        }

        /* internal */ /* var */_blueTf: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_coinIcon: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_goldIcon: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_coinImg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_goldImg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_item: BaseItem;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_levelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_priceTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_sellerTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_chatBtn: GButton;

        /* internal */ /* var */_buyBtn: GButton;

        /* internal */ /* var */_mailBtn: GButton;

        /* internal */ /* var */_marketId: number;

        /* internal */ /* var */_ownerName: string;

        /* internal */ /* var */_sMarketItem: SMarketItem;
    }
